﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ComicTracker.Models;

namespace ComicTracker.Controllers
{ 
    public class SeriesController : Controller
    {
        private ComicTrackerContext db = new ComicTrackerContext();

        //
        // GET: /Series/

        public ViewResult Index()
        {
            var series = db.Series.Include(s => s.Publisher).ToList();
            series.Sort();
            return View(series);
        }

        //
        // GET: /Series/Details/5

        public ViewResult Details(Guid id)
        {
            Series series = db.Series.Find(id);
            return View(series);
        }

        //
        // GET: /Series/Create

        public ActionResult Create(Guid? publisherID = null)
        {
            if (publisherID != null)
                ViewBag.PublisherID = new SelectList(db.Publishers, "ID", "Name", publisherID.Value.ToString());
            else
                ViewBag.PublisherID = new SelectList(db.Publishers, "ID", "Name");
            ViewBag.PublishFrequency = new SelectList(new List<string>{ "Annually", "Bi-Monthly", "Bi-Weekly", "Daily", "Irregular", 
                                                                        "Monthly", "One-Shot", "Quarterly", "Weekly", "Yearly" }, "PublishFrequency");
            return View();
        } 

        //
        // POST: /Series/Create

        [HttpPost]
        public ActionResult Create(Series series)
        {
            if (ModelState.IsValid)
            {
                series.ID = Guid.NewGuid();
                db.Series.Add(series);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.PublisherID = new SelectList(db.Publishers, "ID", "Name", series.PublisherID);
            ViewBag.PublishFrequency = new SelectList(new List<string>{ "Annually", "Bi-Monthly", "Bi-Weekly", "Daily", "Irregular", 
                                                                        "Monthly", "One-Shot", "Quarterly", "Weekly", "Yearly" }, "PublishFrequency");
            return View(series);
        }
        
        //
        // GET: /Series/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Series series = db.Series.Find(id);
            ViewBag.PublisherID = new SelectList(db.Publishers, "ID", "Name", series.PublisherID);
            ViewBag.PublishFrequency = new SelectList(new List<string>{ "Annually", "Bi-Monthly", "Bi-Weekly", "Daily", "Irregular", 
                                                                        "Monthly", "One-Shot", "Quarterly", "Weekly", "Yearly" }, "PublishFrequency");
            return View(series);
        }

        //
        // POST: /Series/Edit/5

        [HttpPost]
        public ActionResult Edit(Series series)
        {
            if (ModelState.IsValid)
            {
                db.Entry(series).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PublisherID = new SelectList(db.Publishers, "ID", "Name", series.PublisherID);
            return View(series);
        }

        //
        // GET: /Series/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Series series = db.Series.Find(id);
            return View(series);
        }

        //
        // POST: /Series/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            Series series = db.Series.Find(id);
            db.Series.Remove(series);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}